<?php

/**
 * page actions.
 *
 * @package    blogallery
 * @subpackage page
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class pageActions extends sfActions
{
  public function executeIndex()
  {
    return $this->forward('page', 'list');
  }

  public function executeList()
  {
    $this->processSort();

    $this->processFilters();

    $this->filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/page/filters');

    // pager
    $this->pager = new sfPropelPager('Page', 20);
    $c = new Criteria();
    $this->addSortCriteria($c);
    $this->addFiltersCriteria($c);
    $this->pager->setCriteria($c);
    $this->pager->setPage($this->getRequestParameter('page', 1));
    $this->pager->init();
  }

  public function executeCreate()
  {
    return $this->forward('page', 'edit');
  }

  public function executeSave()
  {
    return $this->forward('page', 'edit');
  }

  public function executeEdit()
  {
    $this->languages = LanguagePeer::doSelect(new Criteria());
    $this->page = $this->getPageOrCreate();

    if ($this->getRequest()->getMethod() == sfRequest::POST)
    {
      $this->updatePageFromRequest();

      $this->savePage($this->page);

      $this->setFlash('notice', 'Your modifications have been saved');

      if ($this->getRequestParameter('save_and_add'))
      {
        return $this->redirect('page/create');
      }
      else if ($this->getRequestParameter('save_and_list'))
      {
        return $this->redirect('page/list');
      }
      else
      {
        return $this->redirect('page/edit?id='.$this->page->getId());
      }
    }
    else
    {
      $this->labels = $this->getLabels();
    }
  }

  public function executeDelete()
  {
    $this->page = PagePeer::retrieveByPk($this->getRequestParameter('id'));
    $this->forward404Unless($this->page);

    try
    {
      $this->deletePage($this->page);
    }
    catch (PropelException $e)
    {
      $this->getRequest()->setError('delete', 'Could not delete the selected Page. Make sure it does not have any associated items.');
      return $this->forward('page', 'list');
    }

    return $this->redirect('page/list');
  }

  public function handleErrorEdit()
  {
    $this->preExecute();
    $this->page = $this->getPageOrCreate();
    $this->updatePageFromRequest();

    $this->labels = $this->getLabels();

    return sfView::SUCCESS;
  }

  protected function savePage($page)
  {
    $page->save();

  }

  protected function deletePage($page)
  {
    $page->delete();
  }

  protected function updatePageFromRequest()
  {
    $page = $this->getRequestParameter('page');

    if (isset($page['handle']))
    {
      $this->page->setHandle($page['handle']);
    }
    if (isset($page['i18n']))
    {
      $this->setI18n($page['i18n']);
    }
  }
  
  protected function setI18n($i18ns)
  {
    //set i18n page fields
    foreach($i18ns as $culture => $i18n)
    {
      $page_i18n = PageI18nPeer::retrieveByPk($this->page->getId(), $culture);
      
      if(!$page_i18n instanceof PostI18n)
      {
        $page_i18n = new PageI18n();
      }
      
      $page_i18n->setId($this->page->getId());
      $page_i18n->setCulture($culture);      
      $page_i18n->setTitle($i18n['title']);      
      $page_i18n->setContent($i18n['content']); 
      $page_i18n->save();
    }       
  }

  protected function getPageOrCreate($id = 'id')
  {
    if (!$this->getRequestParameter($id))
    {
      $page = new Page();
    }
    else
    {
      $page = PagePeer::retrieveByPk($this->getRequestParameter($id));

      $this->forward404Unless($page);
    }

    return $page;
  }

  protected function processFilters()
  {
    if ($this->getRequest()->hasParameter('filter'))
    {
      $filters = $this->getRequestParameter('filters');

      $this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/page/filters');
      $this->getUser()->getAttributeHolder()->add($filters, 'sf_admin/page/filters');
    }
  }

  protected function processSort()
  {
    if ($this->getRequestParameter('sort'))
    {
      $this->getUser()->setAttribute('sort', $this->getRequestParameter('sort'), 'sf_admin/page/sort');
      $this->getUser()->setAttribute('type', $this->getRequestParameter('type', 'asc'), 'sf_admin/page/sort');
    }

    if (!$this->getUser()->getAttribute('sort', null, 'sf_admin/page/sort'))
    {
    }
  }

  protected function addFiltersCriteria($c)
  {
    if (isset($this->filters['handle_is_empty']))
    {
      $criterion = $c->getNewCriterion(PagePeer::HANDLE, '');
      $criterion->addOr($c->getNewCriterion(PagePeer::HANDLE, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['handle']) && $this->filters['handle'] !== '')
    {
      $c->add(PagePeer::HANDLE, strtr($this->filters['handle'], '*', '%'), Criteria::LIKE);
    }
  }

  protected function addSortCriteria($c)
  {
    if ($sort_column = $this->getUser()->getAttribute('sort', null, 'sf_admin/page/sort'))
    {
      $sort_column = PagePeer::translateFieldName($sort_column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
      if ($this->getUser()->getAttribute('type', null, 'sf_admin/page/sort') == 'asc')
      {
        $c->addAscendingOrderByColumn($sort_column);
      }
      else
      {
        $c->addDescendingOrderByColumn($sort_column);
      }
    }
  }

  protected function getLabels()
  {
    return array(
      'page{handle}' => 'Handle:',
      'page{i18n}' => 'I18n:',
    );
  }
}
